package ricky.game
{
	import ricky.Interface.IAct;
	import ricky.Interface.IGraphic;
	import ricky.game.action.Act2D;
	import ricky.quadtree.Morton;
	
	public class Actor2D extends Actor implements IAct
	{
		public function Actor2D(graphic:IGraphic=null)
		{
			super(graphic);
		}
		
		
		public function get netDelay():Number
		{
			return _netDelay;
		}

		public function set netDelay(value:Number):void
		{
			_netDelay = value;
		}

		/**
		 * 逻辑计算
		 */
		public function action(timeline:Number):void
		{
				
			if(this._isLock)
				return;
			
			if(this._act)
				_act.action(timeline);
			
		}
		
//		/**
//		 * 物品的格子x世界坐标
//		 */
//		public function get gridX():int
//		{
//			_gridX=this.graphic.x/Common.tileWidth;
//			return _gridX;//this.x/Common.tileWidth;
//		}
//		
//		public function set gridX(value:int):void
//		{
//			this.graphic.x = value*Common.tileWidth;
//			this._gridX=value;
//		}
//		
//		/**
//		 * 物品的格子Y世界坐标
//		 */
//		public function get gridY():int
//		{ 
//			_gridY=this.graphic.y/Common.tileHeight;
//			return _gridY;//this.y/Common.tileHeight;
//		}
//		
//		public function set gridY(value:int):void
//		{
//			this.graphic.y = value*Common.tileHeight;
//			_gridY=value;
//		}
		
		override 	public function set isLock(value:Boolean):void
		{
			_isLock = value;
			if(_isLock==false)
			{
				if(_act)
				{
					_act.completeFun=this.actComplete;
					_act.begin();
				}
			}
		}
		
		public function wear(model:String=null,name:String=null,onLoaded:Function=null):void
		{
			if(model)
				graphic.wear(model);
			else
				graphic.wear(_data.model);
		}
		
		/**
		 * 添加行动列表
		 * @param act:Act 行动链表
		 * return true添加成功 false添加失败
		 */
		public function addAct(act:Act2D):Boolean
		{
			if(this._isDead) //如果死亡不能在行动
			{
				return false;
			}
			
			if(_act)
			{
				_act.end();
				_act.dispose();
				_act=null;
			}
			
			_act=act;
			
			if(_isLock) //如果锁定了逻辑运算
				return false;
			
			if(_act)
			{
				_act.completeFun=this.actComplete;
				_act.begin();
			}
			return true;
		}
		
		/**
		 * 行动结束响应函数
		 */ 
		protected function actComplete(e:Act2D):void
		{
			//		
			if(e.next)
			{
			 	this.addAct(e.next);
			}
			else
			
			{
				e.end();
				//				var per:Act=this.perception("actEnd",e);//行动结束
				//				if(per)
				//					addAct(per);
				//				else
				{
					if(!defaultAct)
					{
						defaultAct=new Act2D(this);
						//						defaultAct.actor=this;
						defaultAct.animProperty=Act2D.stand;
					}
					defaultAct.actor=this;
					this.addAct(Act2D(defaultAct));//播放动画
				}
			}
		}
		
		/**
		 * 上次位置码
		 */
		public function get lastMorton():int
		{
			return _lastMorton;
		}
		
		/**
		 * @private
		 */
		public function set lastMorton(value:int):void
		{
			_lastMorton = value;
		}
		
		/**
		 * 定位码
		 */
		public function set morton(value:int):void
		{
			_morton = value;
		}
		
		/**
		 * 定位码
		 */
		public function get morton():int
		{
			return _morton;
		}
		
		/**
		 * 计算定位码
		 */
		public function calcMorton():void
		{
			_morton=Morton.morton(this.gridY,this.gridX);
		}
		
		/**
		 * 默认没得方向 -1
		 * 行动的方向 顺时针方向 0点-7点 点数按象限和xy轴分
		 */
		public function get direction():*
		{
			return _direction;
		}
		
		/**
		 * @private
		 */
		public function set direction(value:*):void
		{
			_direction = value;
		}
		
		
		public function get speed():Number
		{
			return _speed;
		}
		
		public function set speed(value:Number):void
		{
			_speed = value;
		}
		
		public function loadProperty(level:int):void
		{
			
		}
		
		protected var _morton:int;
		protected var _lastMorton:int;
//		protected var _gridY:int=0;
//		protected var _gridX:int=0;
		protected var _direction:*=-1;
		private var _speed:Number=50;//
		private var _netDelay:Number;

		public var defaultAct:Act2D;
	}
}